﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DIProject.Data
{
    public class Party
    {
        public static Int32 GetPartyCount(Int32 partyId)
        {
            DataSets.UtilTableAdapters.UtilQueries adapter = new DIProject.DataSets.UtilTableAdapters.UtilQueries();
            object o = adapter.GetPartyCount(partyId);
            if (!Convert.IsDBNull(o))
                return Convert.ToInt32(o);
            return 0;
        }
        /// <summary>
        /// находим тот приходный документ от поставщика из иерархии, из которого пришел товар
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Int32 GetParentId(Int32 id)
        {
            string QueryString = @"with data(id,pid)
as
(
select id, parentid from CA.Documents as d1 where id = @id--  200002539
union all
select d2.id, d2.parentid from CA.Documents as d2 inner join data on d2.id = data.PId
)
select top 1 id from data where pid is null";
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            using (SqlConnection connection = new SqlConnection(conStr))
            {
                try
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    SqlParameter pId = new SqlParameter("@id", SqlDbType.Int);
                    pId.Value = id;
                    command.Parameters.Add(pId);
                    connection.Open();
                    object obj = command.ExecuteScalar();
                    if (Convert.IsDBNull(obj) || obj == null)
                        return 0;
                    return (Int32)obj;
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить номер накладной\n" + ee.Message);
                    return 0;
                }
            }
        }
        /// <summary>
        /// номер документа партии по ее Id
        /// </summary>
        /// <param name="partyId"></param>
        /// <returns></returns>
        public static Int32 GetPartyDocId(Int32 partyId)
        {
            string QueryString = @"SELECT DocId FROM CD.Parties WHERE Id = @Id";
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            using (SqlConnection connection = new SqlConnection(conString))
            {
                try
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    command.Parameters.AddWithValue("@Id", partyId);
                    connection.Open();
                    object obj = command.ExecuteScalar();
                    if (Convert.IsDBNull(obj) || obj == null)
                        return -1;
                    return Convert.ToInt32(obj);
                }
                catch (SqlException se)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить Id документа партии\n" + se.ToString());
                    return -1;
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить Id документа партии\n" + ee.ToString());
                    return -1;
                }
            }
        }
        public static Int32 GetDepartmentIdFromDocument(Int32 docId)
        {
            string QueryString = @"select DepartmentId 
from CA.Documents
where Id = @Id";
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            using (SqlConnection connection = new SqlConnection(conString))
            {
                try
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    SqlParameter PdocId = new SqlParameter("@Id", SqlDbType.Int);
                    PdocId.Value = docId;
                    command.Parameters.Add(PdocId);
                    connection.Open();
                    object obj = command.ExecuteScalar();
                    if (Convert.IsDBNull(obj) || obj == null)
                        return -1;
                    return Convert.ToInt32(obj);
                }
                catch (SqlException se)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить департамент подразделения\n" + se.ToString());
                    return -1;
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить департамент подразделения\n" + ee.ToString());
                    return -1;
                }
            }
        }
        /// <summary>
        /// контрагент по номеру накладной
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public static Int32 GetContragentIdFromDocument(Int32 docId)
        {
            string QueryString = @"SELECT     ContragentId
FROM         CA.Documents
WHERE     (Id = @id)";
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            using (SqlConnection connection = new SqlConnection(conStr))
            {
                try
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    SqlParameter pId = new SqlParameter("@id", SqlDbType.Int);
                    pId.Value = docId;
                    command.Parameters.Add(pId);
                    connection.Open();
                    object obj = command.ExecuteScalar();
                    if (Convert.IsDBNull(obj) || obj == null)
                        return -1;
                    return (Int32)obj;
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Нельзя получить номер накладной\n" + ee.Message);
                    return -1;
                }
            }
        }
    }
}
